package com.tanhua.dubbo.api;

import com.tanhua.model.mongo.Announcement;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;


@Slf4j
@DubboService
public class AnnouncementsApiImpl implements AnnouncementsApi {


    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public List<Announcement> getAnnouncements(int page, int pagesize) {
        /*Query query = Query.query(
                        Criteria.where("createDate").all()).skip((page - 1) * pagesize)
                .limit(pagesize).with(Sort.by(Sort.Order.desc("createDate")));*/
        Query query = new Query();
        query.skip((page - 1) * pagesize)
                .limit(pagesize).with(Sort.by(Sort.Order.desc("createDate")));
        List<Announcement> announcementList = mongoTemplate.find(query, Announcement.class);
        return announcementList;
    }
}
